package com.open.party.cloud.view.task

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import cn.android.x.plugins.webView.CommWebDetailsActivity
import cn.android.x.plugins.webView.WebViewForFilePreviewActivity
import com.open.party.cloud.R
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.FileEntity
import com.open.party.cloud.model.QueryFileEntity
import com.open.party.cloud.model.data.Biz
import com.open.party.cloud.model.task.TaskRateEntity
import com.open.party.cloud.view.comm.utils.CommonUtils
import com.open.party.cloud.view.task.adapter.EmergencyPhotoAdapter
import com.open.party.cloud.view.task.adapter.SimpleBaseAdapter
import com.open.party.cloud.viewModel.FileViewModel
import com.open.party.cloud.viewModel.TaskViewModel
import com.sinothk.android.utils.ToastUtil
import com.sinothk.android.utils.XUtils
import com.sinothk.dialog.loading.LoadingDialog
import kotlinx.android.synthetic.main.activity_rate_detail.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

/**
 * 任务进度详情
 */
class RateDetailActivity : AppEventBusTitleBaseActivity(), SimpleBaseAdapter.OnAdapterItemClickListener<FileEntity>, View.OnClickListener {
    private var id = ""
    private var isShow: Boolean = false
    private var taskPresenter: TaskViewModel? = null
//    private var commPresenter: FileViewModel? = null

    private var emergencyListAdapter: EmergencyPhotoAdapter? = null

    override fun getLayoutResId(): Int = R.layout.activity_rate_detail

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("进度详情")

        init()
    }

    private fun init() {
        id = intent.getStringExtra("id") as String

        taskPresenter = TaskViewModel()
        taskPresenter!!.loadRateDetail(id)

        emergencyListAdapter = EmergencyPhotoAdapter(this)
        imgGridView.adapter = emergencyListAdapter

        emergencyListAdapter!!.setOnAdapterItemClickListener(this)

        isShow = true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<TaskRateEntity>) {

        if ("loadRateDetail" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                showData(result.data)
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showData(taskRateEntity: TaskRateEntity) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            descTv.text = Html.fromHtml(XUtils.string().getNotNullValue(taskRateEntity.explain), Html.FROM_HTML_MODE_LEGACY)
//        } else {
//            descTv.text = Html.fromHtml(XUtils.string().getNotNullValue(taskRateEntity.explain))
//        }

        CommonUtils.setSpanContentColor(
            this,
            uploadDateTv,
            "上传时间: ",
            R.color.color_B3B3B3,
            taskRateEntity.endTime,
            R.color.color_9A9A9A
        )
//        CommonUtils.setSpanContentColor(
//            this,
//            rateTv,
//            "完成百分比: ",
//            R.color.color_B3B3B3,
//            taskRateEntity.rate.toString() + "%",
//            R.color.color_9A9A9A
//        )
        CommonUtils.setSpanContentColor(
            this,
            rateDescTv,
            "进度描述: ",
            R.color.color_B3B3B3,
            taskRateEntity.description,
            R.color.color_9A9A9A
        )

        if (taskRateEntity.fileList != null && taskRateEntity.fileList.isNotEmpty()) {
            val imgList = ArrayList<FileEntity>()
            val fileList = ArrayList<FileEntity>()

            for (fEntity in taskRateEntity.fileList) {

                val fileEntity = FileEntity()
                fileEntity.submittedFileName = fEntity.fileName
                fileEntity.filePath = fEntity.filePath

                val ext = fEntity.filePath.substring(fEntity.filePath.lastIndexOf('.'))
                fileEntity.ext = ext

                if (fileEntity.ext.contains("png") || fileEntity.ext.contains("jpg") ||
                    fileEntity.ext.contains("jpeg")
                ) {
                    imgList.add(fileEntity)
                } else {

                    fileList.add(fileEntity)
                }
            }

            if (imgList.isNotEmpty()) {
                imgGridView.visibility = View.VISIBLE
                emergencyListAdapter!!.data = imgList
            } else {
                imgGridView.visibility = View.GONE
            }
            if (fileList.isNotEmpty()) {
                rateFileList.visibility = View.VISIBLE

                rateFileList.setData(fileList) { entity ->
                    WebViewForFilePreviewActivity.start(this@RateDetailActivity, entity.submittedFileName, ServerConfig.getFilePreviewUrl(entity.filePath))
                }
            }else{
                rateFileList.visibility = View.GONE
            }
        } else {
            rateFileList.visibility = View.GONE
        }
    }

    override fun onAdapterItemClick(position: Int, data: FileEntity) {
        WebViewForFilePreviewActivity.start(this@RateDetailActivity, data.submittedFileName, ServerConfig.getFilePreviewUrl(data.filePath))
    }

    override fun onClick(v: View?) {
        when (v) {
//            bottomView -> {
//                XUtils.intent().openActivity(this@RateDetailActivity, TaskCheckAddActivity::class.java)
//                    .putStringExtra("id", id)
//                    .start()
//            }
        }
    }
}